﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;

namespace AndrewTicTacToe
{
    class GameServer
    {
        public IPAddress ipAddress = IPAddress.Any;
        private TcpClient client;

        //List containing.. connection?? Why?
        public List<Connection> clients { get; private set; }

        private bool clientIsConnected = false;

        public GameServer()
        {
            clients = new List<Connection>();
            TcpListener listener = new TcpListener(ipAddress, 1300);
            listener.Start();
            System.Diagnostics.Debug.WriteLine("Waiting for a damn connection");
            while (clientIsConnected == false)
            {
                //listen to clients and accept them
                client = listener.AcceptTcpClient();
    
                System.Diagnostics.Debug.WriteLine("Connection established");

                Connection handle = new Connection(this);

                handle.setClient(client);

                clients.Add(handle);
                System.Diagnostics.Debug.WriteLine("Client has connected to Server");
                System.Diagnostics.Debug.WriteLine("Amount of Clients in list: " + clients.Count);
                if (clients.Count == 1)
                {//Stop listening to clients
                    clientIsConnected = true;
                }
            }
        }

    }


    //Class for handling connecting clients
    class Connection
    {
        GameServer server;
        public TcpClient client { get; private set; }


        public Connection(GameServer server)
        {
            this.server = server;
        }

        public void setClient(TcpClient client)
        {
            this.client = client;
        }

       
    }
}
